package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.article.domain.Wenda;
import cn.wolfcode.luowowo.article.mapper.WendaMapper;
import cn.wolfcode.luowowo.article.query.WendaQuery;
import cn.wolfcode.luowowo.article.service.IWendaService;
import cn.wolfcode.luowowo.common.exception.LogicException;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class WendaServiceImpl implements IWendaService {
    @Autowired
    private WendaMapper wendaMapper;

    @Override
    public Wenda get(Long id) {
        return wendaMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo query(WendaQuery qo) {
        if (qo.getState() != null && qo.getState() == 1){
            qo.setOrderBy("createTime desc");
        } else if (qo.getState() != null && qo.getState() == -1){
            qo.setOrderBy("createTime asc");
        }
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(wendaMapper.selectForList(qo));
    }

    @Override
    public void saveOrUpdate(Wenda wenda) {

        if (wenda.getTitle() == null || wenda.getTitle().length() < 10){
            throw new LogicException("请输入问题标题,且字数不能少于10个");
        }
        if (wenda.getContent() == null){
            throw new LogicException("请描述你的问题");
        }

        if(wenda.getId() == null){
            wenda.setCreateTime(new Date());
            wendaMapper.insert(wenda);
        }else {
            wendaMapper.updateByPrimaryKey(wenda);
        }
    }

    @Override
    public void delete(Long id) {
        wendaMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Wenda> selectByDestIdT5(Long destId, Long outId) {
        List<Wenda> list = wendaMapper.selectByDestIdT5(destId, outId);
        return list;
    }

    @Override
    public void updateNum(Wenda wenda) {
        wendaMapper.updateNum(wenda);
    }

    @Override
    public List<Wenda> list() {
        return wendaMapper.selectAll();
    }
}
